package com.example.meituan.dao;

import android.annotation.SuppressLint;
import android.content.ContentValues;
import android.content.Context;
import android.content.SharedPreferences;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

import com.example.meituan.activity.bean.AdminBean;
import com.example.meituan.activity.bean.BusinessBean;
import com.example.meituan.db.DBUntil;

/*
* 操作数据库
* */
public class AdminDao {
    private static SQLiteDatabase db=DBUntil.con;
    public static int saveBusinessUser(String id,String pwd,String name,String des,String type,String tx) {
        String data[] = {id, pwd, name, des, type, tx};
        try {
            db.execSQL("INSERT INTO d_business (s_id, s_pwd, s_name, s_describe, s_type, s_img)" +
                            "VALUES (?,?,?,?,?,?)",
                    data);
            return 1;
        }catch (Exception e){
            return 0;
        }
    }

    public static int saveUser(String id,String pwd,String name,String phone,String shen,String tx) {
        String data[] = {id, pwd, name, phone, shen, tx};
        try {
            db.execSQL("INSERT INTO d_user (s_id, s_pwd, s_name, s_phone, s_shen, s_img)" +
                            "VALUES (?,?,?,?,?,?)",
                    data);
            return 1;
        }catch (Exception e){
            return 0;
        }
    }

    public static int loginBusiness(String account, String pwd) {
        // 先对传入的密码进行加密
        String encryptedPwd = JiaMi.encryptPassword(pwd);
        if (encryptedPwd == null) {
            // 如果加密过程出现异常（比如算法不存在等极端情况），直接返回错误
            return 0;
        }
        String sql = "Select * from d_business where s_id=? and s_pwd=?";
        String data[] = {account, encryptedPwd};
        Cursor result = db.rawQuery(sql, data);
        while (result.moveToNext()) {
            return 1;
        }
        return 0;
    }

    public static int loginUser(String account, String pwd) {
        // 先对传入的密码进行加密
        String encryptedPwd = JiaMi.encryptPassword(pwd);
        if (encryptedPwd == null) {
            // 如果加密过程出现异常，直接返回错误
            return 0;
        }
        String sql = "Select * from d_user where s_id=? and s_pwd=?";
        String data[] = {account, encryptedPwd};
        Cursor result = db.rawQuery(sql, data);
        while (result.moveToNext()) {
            return 1;
        }
        return 0;
    }

    public static AdminBean getUserInfo(Context context) {
        SharedPreferences sharedPreferences = context.getSharedPreferences("user_prefs", Context.MODE_PRIVATE);

        String userId = sharedPreferences.getString("user_id", "");
        boolean isLoggedIn = sharedPreferences.getBoolean("is_logged_in", false);

        if (isLoggedIn) {
            return new AdminBean(userId);
        } else {
            return null;
        }
    }

    public static BusinessBean getBusiness(String id){
        BusinessBean businessBean=new BusinessBean();
        Cursor cursor=db.rawQuery("select * from d_business where s_id=?",new String[]{id});
        if (cursor.moveToNext()){
            @SuppressLint("Range") String pwd=cursor.getString(cursor.getColumnIndex("s_pwd"));
            @SuppressLint("Range") String name=cursor.getString(cursor.getColumnIndex("s_name"));
            @SuppressLint("Range") String des=cursor.getString(cursor.getColumnIndex("s_describe"));
            @SuppressLint("Range") String type=cursor.getString(cursor.getColumnIndex("s_type"));
            @SuppressLint("Range") String img=cursor.getString(cursor.getColumnIndex("s_img"));
            businessBean.setsDescribe(des);
            businessBean.setsId(id);
            businessBean.setsImg(img);
            businessBean.setsPwd(pwd);
            businessBean.setsName(name);
            businessBean.setsType(type);
        }else{
            return null;
        }
        cursor.close();
        return businessBean;
    }

    // 删除用户
    public static int deleteBusinesssById(String id) {
        try {
            db.delete("d_business", "s_id=?", new String[]{id});
            return 1;
        }
        catch(Exception e){
            return 0;
        }
    }

    //更新店铺信息
    public static int updateStore(BusinessBean businessBean) {
        ContentValues values = new ContentValues();
        values.put("s_id", businessBean.getsId());
        values.put("s_pwd", businessBean.getsPwd());
        values.put("s_name", businessBean.getsName());
        values.put("s_describe", businessBean.getsDescribe());
        values.put("s_type", businessBean.getsType());
        values.put("s_img", businessBean.getsImg());

        try {
            db.update("d_business", values, "s_id=?", new String[]{businessBean.getsId()});
            return 1;
        }
        catch (Exception e)
        {
            return 0;
        }
    }
}
